package com.zxw.article.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxw.article.mapper.TagMapper;
import com.zxw.article.service.TagService;
import com.zxw.common.entity.tag.TagEntity;
import com.zxw.common.entity.tag.TagListQuery;
import com.zxw.common.entity.tag.TagQuery;
import com.zxw.common.entity.tag.TagVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签信息表 服务实现类
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-09-19
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, TagEntity> implements TagService {


    /**
     * 添加
     *
     * @param tagQuery TagQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean save(TagQuery tagQuery) {
        TagEntity tagEntity = new TagEntity();
        BeanUtils.copyProperties(tagQuery, tagEntity);
        tagEntity.setId(null);
        tagEntity.setStatus("1");
        return super.save(tagEntity);
    }

    /**
     * 批量添加
     *
     * @param list List<TagQuery>
     * @return 是否成功 true|false
     */
    @Override
    public boolean saveBatch(List<TagQuery> list) {
        List<TagEntity> collect = list.stream().map(item -> {
            TagEntity tagEntity = new TagEntity();
            BeanUtils.copyProperties(item, tagEntity);
            tagEntity.setId(null);
            tagEntity.setStatus("1");
            return tagEntity;
        }).collect(Collectors.toList());
        return super.saveBatch(collect);
    }

    /**
     * 根据ID集合批量删除
     *
     * @param ids id集合
     * @return 是否成功 true|false
     */
    @Override
    public boolean removeByIds(List<String> ids) {
        return super.removeByIds(ids);
    }

    /**
     * 根据ID修改
     *
     * @param tagQuery TagQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean updateById(TagQuery tagQuery) {
        TagEntity tagEntity = new TagEntity();
        BeanUtils.copyProperties(tagQuery, tagEntity);
        return super.updateById(tagEntity);
    }

    /**
     * 根据ID查询数据
     *
     * @param id ID
     * @return TagDO
     */
    @Override
    public TagEntity getById(String id) {
        return super.getById(id);
    }

    /**
     * 条件分页查询
     *
     * @param tagListQuery TagListQuery
     * @return IPage<TagDO>
     */
    @Override
    public IPage<TagEntity> list(TagListQuery tagListQuery) {
        IPage<TagEntity> pageInfo = new Page<>(tagListQuery.getCurrent(), tagListQuery.getSize());
        QueryWrapper<TagEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TagEntity::getStatus, 1)
                .orderByDesc(TagEntity::getCreateTime);
        return super.page(pageInfo, queryWrapper);
    }

    @Override
    public IPage<TagVO> pageList(TagListQuery tagListQuery) {
        IPage<TagEntity> pageInfo = new Page<>(tagListQuery.getCurrent(), tagListQuery.getSize());
        QueryWrapper<TagEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TagEntity> lambda = queryWrapper.lambda();
        if (StringUtils.isNotBlank(tagListQuery.getName())) {
            lambda.like(TagEntity::getName, tagListQuery.getName());
        }
        if (StringUtils.isNotBlank(tagListQuery.getType())) {
            lambda.eq(TagEntity::getType, tagListQuery.getType());
        }
        lambda.orderByDesc(TagEntity::getCreateTime);
        IPage<TagEntity> page = super.page(pageInfo, queryWrapper);

        return page.convert(tagEntity -> {
            TagVO tagVO = new TagVO();
            BeanUtils.copyProperties(tagEntity, tagVO);
            return tagVO;
        });

    }

    @Override
    public List<TagVO> queryList() {
        QueryWrapper<TagEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TagEntity> lambda = queryWrapper.lambda();
        lambda.orderByDesc(TagEntity::getCreateTime);
        List<TagEntity> list = super.list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            return list.stream().map(e -> {
                TagVO tagVO = new TagVO();
                BeanUtils.copyProperties(e, tagVO);
                return tagVO;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

}
